﻿using EFCoreDemo2.Entity;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;

namespace EFCoreDemo2
{
    public class SqlDbContext:DbContext
    {
        private static ILoggerFactory logger = LoggerFactory.Create(builder => builder.AddConsole());
        public DbSet<Student> Students { get; set; }
        public DbSet<Subject> Subject { get; set; }
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            base.OnConfiguring(optionsBuilder);
            optionsBuilder.UseSqlServer("Server=localhost;Database=DB7-9;Integrated Security=True;Encrypt=False;");
            optionsBuilder.UseLoggerFactory(logger);
           // optionsBuilder.LogTo(PrintSql);
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            modelBuilder.ApplyConfigurationsFromAssembly(this.GetType().Assembly);
        }

        private void PrintSql(string msg)
        {
            if(msg.Contains("info:"))
            {
                Console.WriteLine(msg);
            }
        }
    }
}
